package com.yy.micro.magic.core.provider.impl;

import com.yy.micro.magic.ext.domain.filter.DynamicTrigger;

import org.springframework.stereotype.Component;

import java.util.Optional;
import java.util.Set;

public abstract class TriggerResourceProvider extends AbstractResourceProvider<Set<DynamicTrigger>>{

    public DynamicTrigger find(String planCode, String condition){
        Set<DynamicTrigger> conditions = find(planCode);
        if (null != conditions){
            Optional<DynamicTrigger> fc= conditions.stream().filter(c->c.match(planCode,condition)).findFirst();
            if (fc.isPresent()){
                return fc.get();
            }
        }
        return null;
    }


    @Override
    public void regist(final String code, final Set<DynamicTrigger> entity) {
        Set<DynamicTrigger> triggers = find(code);
        if (null != triggers){
            triggers.addAll(entity);
        }else {
            super.regist(code, entity);
        }
    }

    @Component("conditionImpl")
    public static class Impl extends TriggerResourceProvider {

    }
}
